package com.application.fabric.fabriccontrol.existevidence;

import com.application.fabric.fabriccontrol.config.Accountpath;
import com.application.fabric.fabriccontrol.config.Orgcermpath;
import org.hyperledger.fabric.gateway.Wallet;
import org.hyperledger.fabric.gateway.Wallets;
import org.hyperledger.fabric.gateway.X509Identity;
import org.hyperledger.fabric.sdk.security.CryptoSuite;
import org.hyperledger.fabric.sdk.security.CryptoSuiteFactory;
import org.hyperledger.fabric_ca.sdk.HFCAClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.nio.file.Paths;
import java.security.MessageDigest;
import java.security.PrivateKey;
import java.security.cert.X509Certificate;
import java.util.Base64;
import java.util.Properties;

@Repository
public class getcacert {
    /*返回ca证书hash*/
    @Autowired
    private Orgcermpath orgcermpath;
    @Autowired
    private Accountpath accountpath;

    public String cacermbase64() throws Exception{
        Properties props = new Properties();
        props.put("pemFile",orgcermpath.getOrg1());
        props.put("allowAllHostNames","true");
        HFCAClient caClient = HFCAClient.createNewInstance("https://localhost:7054", props);
        CryptoSuite cryptoSuite = CryptoSuiteFactory.getDefault().getCryptoSuite();
        caClient.setCryptoSuite(cryptoSuite);
        Wallet wallet = Wallets.newFileSystemWallet(Paths.get(accountpath.getWallet()));
        X509Identity adminIdentity = (X509Identity)wallet.get("admin");
        System.out.println(adminIdentity.getCertificate());
        X509Certificate cer = adminIdentity.getCertificate();
        String str = Base64.getEncoder().encodeToString(cer.getEncoded());
        return str;
    }
    /*破坏性md5 获取用户id*/
    public String getcermsd5Id() throws Exception{
        /*破坏性md5*/
        String str = cacermbase64();
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] digestBytes = md.digest(str.getBytes());
        StringBuilder sb = new StringBuilder();
        for (byte b : digestBytes) {
            sb.append(String.format("%02x", b & 0xff));
        }
        System.out.println("MD5 hash: " + sb.toString());
        return sb.toString();
    }


    /*获取私钥*/
    public String getuserPrikey() throws Exception{
        Properties props = new Properties();
        props.put("pemFile",orgcermpath.getOrg1());
        props.put("allowAllHostNames","true");
        HFCAClient caClient = HFCAClient.createNewInstance("https://localhost:7054", props);
        CryptoSuite cryptoSuite = CryptoSuiteFactory.getDefault().getCryptoSuite();
        caClient.setCryptoSuite(cryptoSuite);
        Wallet wallet = Wallets.newFileSystemWallet(Paths.get(accountpath.getWallet()));
        X509Identity adminIdentity = (X509Identity)wallet.get("admin");
        PrivateKey prikey = adminIdentity.getPrivateKey();
        String key = Base64.getEncoder().encodeToString(prikey.getEncoded());
        System.out.println(key);
        return key;
    }

}
